Completed
Push — master ( 90f02e...53fcb4 )
by Maxence
02:59
created

Circles.initialize   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 226

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 226
rs 8.2857
cc 1
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/*
2
 * Circles - Bring cloud-users closer together.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: OCA */
28
29
(function () {
30
31
32
	/**
33
	 * @constructs Circles
34
	 */
35
	var Circles = function () {
36
		this.initialize();
37
	};
38
39
	Circles.prototype = {
40
41
42
		initialize: function () {
43
44
			var self = this;
45
46
47
			/**
48
			 * API function to create a new Circle.
49
			 *
50
			 * @param type
51
			 * @param name
52
			 * @param callback
53
			 */
54
			this.createCircle = function (type, name, callback) {
55
56
				var result = {status: -1};
57
				$.ajax({
58
					method: 'PUT',
59
					url: OC.generateUrl('/apps/circles/v1/circles'),
60
					data: {
61
						type: type,
62
						name: name
63
					}
64
				}).done(function (res) {
65
					self.onCallback(callback, res);
66
				}).fail(function () {
67
					self.onCallback(callback, result);
68
				});
69
			};
70
71
72
			this.listCircles = function (type, name, level, callback) {
73
				var result = {status: -1};
74
				$.ajax({
75
					method: 'GET',
76
					url: OC.generateUrl('/apps/circles/v1/circles'),
77
					data: {
78
						type: type,
79
						name: name,
80
						level: level
81
					}
82
				}).done(function (res) {
83
					self.onCallback(callback, res);
84
				}).fail(function () {
85
					self.onCallback(callback, result);
86
				});
87
			};
88
89
90
			this.detailsCircle = function (circleId, callback) {
91
				var result = {status: -1};
92
				$.ajax({
93
					method: 'GET',
94
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId)
95
				}).done(function (res) {
96
					self.onCallback(callback, res);
97
				}).fail(function () {
98
					self.onCallback(callback, result);
99
				});
100
			};
101
102
103
			this.addMember = function (circleId, userId, callback) {
104
				var result = {status: -1};
105
				$.ajax({
106
					method: 'PUT',
107
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/members'),
108
					data: {
109
						name: userId
110
					}
111
				}).done(function (res) {
112
					self.onCallback(callback, res);
113
				}).fail(function () {
114
					self.onCallback(callback, result);
115
				});
116
			};
117
118
119
			this.removeMember = function (circleId, userId, callback) {
120
				var result = {status: -1};
121
				$.ajax({
122
					method: 'DELETE',
123
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/members'),
124
					data: {
125
						member: userId
126
					}
127
				}).done(function (res) {
128
					self.onCallback(callback, res);
129
				}).fail(function () {
130
					self.onCallback(callback, result);
131
				});
132
			};
133
134
135
			this.levelMember = function (circleId, member, level, callback) {
136
				var result = {status: -1};
137
				$.ajax({
138
					method: 'POST',
139
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/level'),
140
					data: {
141
						member: member,
142
						level: level
143
					}
144
				}).done(function (res) {
145
					self.onCallback(callback, res);
146
				}).fail(function () {
147
					self.onCallback(callback, result);
148
				});
149
			};
150
151
152
			this.joinCircle = function (circleId, callback) {
153
				var result = {status: -1};
154
				$.ajax({
155
					method: 'GET',
156
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/join'),
157
					data: {}
158
				}).done(function (res) {
159
					self.onCallback(callback, res);
160
				}).fail(function () {
161
					self.onCallback(callback, result);
162
				});
163
			};
164
165
166
			this.settingsCircle = function (circleId, settings, callback) {
167
				var result = {status: -1};
168
				$.ajax({
169
					method: 'POST',
170
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/settings'),
171
					data: {settings: settings}
172
				}).done(function (res) {
173
					self.onCallback(callback, res);
174
				}).fail(function () {
175
					self.onCallback(callback, result);
176
				});
177
			};
178
179
180
			this.leaveCircle = function (circleId, callback) {
181
				var result = {status: -1};
182
				$.ajax({
183
					method: 'GET',
184
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/leave'),
185
					data: {}
186
				}).done(function (res) {
187
					self.onCallback(callback, res);
188
				}).fail(function () {
189
					self.onCallback(callback, result);
190
				});
191
			};
192
193
194
			this.destroyCircle = function (circleId, callback) {
195
				var result = {status: -1};
196
				$.ajax({
197
					method: 'DELETE',
198
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId),
199
					data: {}
200
				}).done(function (res) {
201
					self.onCallback(callback, res);
202
				}).fail(function () {
203
					self.onCallback(callback, result);
204
				});
205
			};
206
207
208
			this.shareToCircle = function (circleId, source, type, item, callback) {
209
				var result = {status: -1};
210
				$.ajax({
211
					method: 'PUT',
212
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/share'),
213
					data: {
214
						source: source,
215
						type: type,
216
						item: item
217
					}
218
				}).done(function (res) {
219
					self.onCallback(callback, res);
220
				}).fail(function () {
221
					self.onCallback(callback, result);
222
				});
223
			};
224
225
226
			this.linkCircle = function (circleId, remote, callback) {
227
				var result = {status: -1};
228
				$.ajax({
229
					method: 'POST',
230
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/link'),
231
					data: {
232
						remote: remote
233
					}
234
				}).done(function (res) {
235
					self.onCallback(callback, res);
236
				}).fail(function () {
237
					self.onCallback(callback, result);
238
				});
239
			};
240
241
242
			this.linkStatus = function(linkId, status, callback) {
243
				var result = {status: -1};
244
				$.ajax({
245
					method: 'POST',
246
					url: OC.generateUrl('/apps/circles/v1/link/' + linkId + '/status'),
247
					data: {
248
						status: status
249
					}
250
				}).done(function (res) {
251
					self.onCallback(callback, res);
252
				}).fail(function () {
253
					self.onCallback(callback, result);
254
				});
255
			};
256
257
			this.onCallback = function (callback, result) {
258
				if (callback && (typeof callback === 'function')) {
259
					if (typeof result === 'object') {
260
						callback(result);
261
					} else {
262
						callback({status: -1})
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
263
					}
264
				}
265
			};
266
267
		}
268
269
	};
270
271
	OCA.Circles = Circles;
272
	OCA.Circles.api = new Circles();
273
274
})();
275
276
277